/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import connection.ConnectionDB;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.CarModel;

/**
 *
 * @author Nhang Con
 */
public class CarModelDAL {

    private ConnectionDB conn = new ConnectionDB();
    private CallableStatement cs;
    private ResultSet rs;

    public boolean newCarModel(CarModel model) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_NewCarModel(?,?)}");
            cs.setString("name",model.getModelName());
            cs.setString("description",model.getDescription());
            int n = cs.executeUpdate();
            if (n == 1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(CarModelDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
        }

    public List<CarModel> getListAll(){

        List<CarModel> list = new ArrayList<CarModel>();
        CarModel model;
        try {
            cs = conn.createConnection().prepareCall("{call sp_ListAllCarModel()}");
            rs = cs.executeQuery();
            while (rs.next()) {
                model = new CarModel();
                model.setModelID(rs.getInt("ModelID"));
                model.setModelName(rs.getString("ModelName"));
                model.setDescription(rs.getString("Description"));
                list.add(model);
            }
            return list;
        } catch (SQLException ex) {
            Logger.getLogger(CarModelDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        return list;
    }


    public boolean updateCarModel(CarModel model) {
        try {
            cs = conn.createConnection().prepareCall("{call sp_UpdateCarModel(?,?,?)}");
            cs.setInt("modelID",model.getModelID());
            cs.setString("name", model.getModelName());
            cs.setString("description ", model.getDescription());
            if (cs.executeUpdate() == 1) {
                return true;
            }
            return false;
        } catch (SQLException ex) {
            Logger.getLogger(CarModelDAL.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

}
